home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / General tools / Report Error 2.0 / TEUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  8.7 KB  |  328 lines  |  [TEXT/KAHL]

  1. /*================================================================================
  2.     TEUtilities.c
  3.         
  4.     ©1991 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     FREE DISTRIBUTION
  8.     
  9.     The possessor of this source code may use any portion of
  10.     it for any purpose, and I place no restriction on its
  11.     redistribution.
  12.  
  13.     Useful routines to use with TextEdit.
  14.     
  15.     These routines support VERTICAL scrollbars only.  Horizontal
  16.     scrollbars are left as an exercise for the reader.
  17.     
  18. ================================================================================*/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <Types.h>
  22. #include <Resources.h>
  23. #include <pascal.h>
  24.  
  25. #include "TEUtilities.h"
  26.  
  27. #ifndef __MEMORY__
  28. #include "Memory.h"
  29. #endif
  30.  
  31. short TELineHeight( TEHandle te );
  32.  
  33. /*----------------------------------------------------------------------
  34.     Get the current selection
  35. ----------------------------------------------------------------------*/
  36. void TEGetSelect( short* selStart, short* selEnd, TEHandle te )
  37. {
  38.     *selStart    = (*te)->selStart;
  39.     *selEnd        = (*te)->selEnd;
  40. }
  41.  
  42. /*----------------------------------------------------------------------
  43.     Get some characters from the textedit record
  44. ----------------------------------------------------------------------*/
  45. void TEGetChars( char* buf, short from, short to, TEHandle te )
  46. {
  47.     CharsHandle        theText;
  48.     short            theState;
  49.     unsigned char*    ptr;
  50.     
  51.     theText = TEGetText(te);
  52.     theState = HGetState( (Handle)theText );
  53.     HLock( (Handle)theText );
  54.     ptr = (unsigned char*)(*theText);
  55.     ptr += from; 
  56.     BlockMove( ptr, buf, to - from );
  57.     buf[to - from] = 0;
  58.     HSetState( (Handle)theText, theState );
  59. }
  60.  
  61. /*----------------------------------------------------------------------
  62.     Return the current view rectangle of the given textedit record
  63. ----------------------------------------------------------------------*/
  64. void GetTEView( TEHandle te, Rect* viewRect )
  65. {
  66.     *viewRect = (*te)->viewRect;
  67. }
  68.  
  69. /*----------------------------------------------------------------------
  70.     Return the number of lines in the current textedit buffer
  71. ----------------------------------------------------------------------*/
  72. short TELines( TEHandle te )
  73. {
  74.     return( (*te)->nLines );
  75. }
  76.  
  77. /*----------------------------------------------------------------------
  78.     Return the pixel height of the current textedit buffer
  79. ----------------------------------------------------------------------*/
  80. short TEHeight( TEHandle te )
  81. {
  82.     return TEGetHeight( TELines(te), 0, te );
  83.     
  84.     #if 0
  85.         return TELines(te) * (*te)->lineHeight;
  86.     #endif
  87. }
  88.  
  89. /*----------------------------------------------------------------------
  90.     Return the # of pixels in one line
  91. ----------------------------------------------------------------------*/
  92. short TELineHeight( TEHandle te )
  93. {
  94.     if( (*te)->lineHeight == -1 )
  95.         return 16;
  96.     
  97.     return (*te)->lineHeight;
  98. }
  99.  
  100. /*----------------------------------------------------------------------
  101.     Return the # of pixels to the current scroll location
  102. ----------------------------------------------------------------------*/
  103. short GetTEVScroll( TEHandle te )
  104. {
  105.     return (*te)->viewRect.top - (*te)->destRect.top;
  106. }
  107.  
  108. /*----------------------------------------------------------------------
  109.     Return the pixel height of the view rectangle
  110. ----------------------------------------------------------------------*/
  111. short ViewHeight( TEHandle te )
  112. {
  113.     Rect        viewRect;
  114.  
  115.     GetTEView( te , &viewRect );
  116.     
  117.     return viewRect.bottom - viewRect.top;
  118. }
  119.  
  120. /*----------------------------------------------------------------------
  121.     Return a number from 1 - 1000 indicating the current scroll location of
  122.     the textedit record
  123. ----------------------------------------------------------------------*/
  124. short VScrollLocation( TEHandle te )
  125. {
  126.     long tmp;
  127.     
  128.     tmp = GetTEVScroll(te) * 1000L;
  129.     tmp /= (TEHeight(te) - ViewHeight(te));
  130.     
  131.     return tmp;
  132. }
  133.  
  134. /*----------------------------------------------------------------------
  135.     Call TEScroll, but don't scroll past top or below bottom
  136.     
  137.     This code also adjusts the scrollbar.
  138. ----------------------------------------------------------------------*/
  139. void PinnedTEVScroll( TEHandle te, ControlHandle scrollbar, short dv )
  140. {
  141.     short    max;
  142.     short    lineh = TELineHeight(te);
  143.     
  144.     if( lineh == -1 )
  145.         lineh = 16;
  146.     
  147.     if( dv > 0 )
  148.     {
  149.         max = GetTEVScroll(te);
  150.         if( dv > max )
  151.             dv = max;
  152.     }
  153.     else
  154.     {
  155.         max = TEHeight(te) - ViewHeight(te) - GetTEVScroll(te) - 1;
  156.         max = max - (max % lineh) + lineh;
  157.         if( max < 0 )
  158.             max = 0;
  159.         if( (-dv) > max )
  160.             dv = -max;
  161.     }
  162.     TEScroll(0,dv,te);
  163.     FixScrollBar( scrollbar );
  164. }
  165.  
  166. /*----------------------------------------------------------------------
  167.     Scroll directly to a new location.
  168. ----------------------------------------------------------------------*/
  169. void TEVThumbScroll( TEHandle te, ControlHandle scrollbar )
  170. {
  171.     long        newScroll;
  172.     short        deltaScroll;
  173.     
  174.     /*
  175.     // 'GetCtlValue' ranges from 0 to 1000.  Convert this into a textedit range
  176.     // of zero to (texteditHeight - viewHeight)
  177.     */
  178.     newScroll  = GetCtlValue( scrollbar );
  179.     newScroll *= (TEHeight(te) - ViewHeight(te));
  180.     newScroll /= 1000;
  181.     /*
  182.     // We don't want to scroll to just any pixel, though...
  183.     */
  184.     newScroll /= TELineHeight(te);
  185.     newScroll *= TELineHeight(te);
  186.     /*
  187.     // Now find out where we are, and how far we have to move to get to
  188.     // where we want to go.
  189.     */
  190.     deltaScroll = GetTEVScroll(te) - newScroll;
  191.     PinnedTEVScroll( te, scrollbar, deltaScroll );
  192. }
  193.  
  194. /*----------------------------------------------------------------------
  195.     Scroll up or down one line
  196. ----------------------------------------------------------------------*/
  197. void ScrollTELine( TEHandle te, ControlHandle scrollbar, short dir )
  198. {
  199.     PinnedTEVScroll( te, scrollbar, dir * TELineHeight(te) );
  200. }
  201.  
  202. /*----------------------------------------------------------------------
  203.     Scroll up or down one page
  204. ----------------------------------------------------------------------*/
  205. void ScrollTEPage( TEHandle te, ControlHandle scrollbar, short dir )
  206. {
  207.     short        linePix;
  208.     short        pagePix;
  209.     short        pageLines;
  210.     
  211.     pagePix = ViewHeight(te);
  212.     linePix = TELineHeight(te);
  213.     pageLines = pagePix / linePix;
  214.     pagePix = (pageLines - 1) * linePix;
  215.     
  216.     PinnedTEVScroll( te, scrollbar, dir * pagePix );
  217. }
  218.  
  219. /*----------------------------------------------------------------------
  220.     This routine is called repeatedly while the scroll bar is tracked.
  221. ----------------------------------------------------------------------*/
  222. pascal TrackUpDownArrows( ControlHandle theControl, short partCode)
  223. {
  224.     TEHandle    te = (TEHandle)GetCRefCon(theControl);
  225.  
  226.     switch( partCode )
  227.     {
  228.         case inUpButton:
  229.         {
  230.             ScrollTELine( te, theControl, 1 );
  231.             break;
  232.         }
  233.         
  234.         case inDownButton:
  235.         {
  236.             ScrollTELine( te, theControl, -1 );
  237.             break;
  238.         }
  239.     }
  240. }
  241.  
  242. /*----------------------------------------------------------------------
  243.     Interact with a VERTICAL scroll bar
  244.     
  245.     Pass a handle to the scrollbar's control record and the point (in LOCAL
  246.     coordinates) where the mouse hit the scrollbar.
  247.     
  248.     WARNING:    The refCon of the scrollbar MUST point to the textedit record
  249.                 That it is associated with.
  250. ----------------------------------------------------------------------*/
  251. void ScrollBarClick( ControlHandle scrollbar, Point where )
  252. {
  253.     TEHandle        te;
  254.     int                value;
  255.     
  256.     /*
  257.     // Ponnuki always sets the refCon of a scrollbar to the
  258.     // TEHandle associated with that scrollbar
  259.     */
  260.     te = (TEHandle) GetCRefCon( scrollbar );
  261.     /*
  262.     // TrackControl passes different parameters to 'trackAction' based on
  263.     // what type of control it is tracking & what part of the control the
  264.     // mouse is in.  Therefore, we cannot simply use the same trackAction
  265.     // procedure for every 'TrackControl' that might come through this
  266.     // routine.    :<  Fortunately, we only need to call a 'trackAction'
  267.     // proc for the up and down facing buttons.
  268.     */
  269.     switch( TestControl(scrollbar,where) )
  270.     {
  271.         case inUpButton:
  272.         case inDownButton:
  273.         {
  274.             TrackControl(scrollbar,where,(ProcPtr)TrackUpDownArrows);
  275.             break;
  276.         }
  277.         
  278.         case inPageUp:
  279.         case inPageDown:
  280.         case inThumb:
  281.         {
  282.             switch( TrackControl(scrollbar,where,(ProcPtr)0L) )
  283.             {
  284.                 case inPageUp:
  285.                 {
  286.                     ScrollTEPage( te, scrollbar, 1 );
  287.                     break;
  288.                 }
  289.                 
  290.                 case inPageDown:
  291.                 {
  292.                     ScrollTEPage( te, scrollbar, -1 );
  293.                     break;
  294.                 }
  295.                 
  296.                 case inThumb:
  297.                 {
  298.                     TEVThumbScroll( te, scrollbar );
  299.                     break;
  300.                 }
  301.             }
  302.             break;
  303.         }
  304.     }
  305. }
  306.  
  307. /*----------------------------------------------------------------------
  308.     Fix a VERTICAL scroll bar after a TE field has been scrolled 
  309.     
  310.     Pass a handle to the scrollbar's control record
  311.     
  312.     WARNING:    The refCon of the scrollbar MUST point to the textedit record
  313.     That it is associated with.
  314. ----------------------------------------------------------------------*/
  315. void FixScrollBar( ControlHandle scrollbar )
  316. {
  317.     TEHandle        te;
  318.     int                value;
  319.     
  320.     /*
  321.     // Ponnuki always sets the refCon of a scrollbar to the
  322.     // TEHandle associated with that scrollbar
  323.     */
  324.     te = (TEHandle) GetCRefCon( scrollbar );
  325.     SetCtlValue( scrollbar, VScrollLocation(te) );
  326.     Draw1Control(scrollbar);
  327. }
  328.